package com.zeen.wima.view;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.zeen.wima.R;
import com.zeen.wima.common.IView;
import com.zeen.wima.common.LocalData;
import com.zeen.wima.common.TraceService;
import com.zeen.wima.common.Tracer;
import com.zeen.wima.common.Wima;

public class InitSetting extends Activity implements IView {

	ProgressDialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		LocalData data = Wima.getInstance().getLocalData();
		if (data.read("registed").equals("1")) {
			setContentView(R.layout.pleasure);
			Button close = (Button) findViewById(R.id.close_button);
			close.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					finish();
				}
			});
		} else {
			setContentView(R.layout.init_setting);
			Button register = (Button) findViewById(R.id.register);
			register.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					register();
				}
			});
		}
	}

	private void register() {
		ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
		// make sure network is available
		if (activeNetInfo == null || !activeNetInfo.isAvailable()) {
			Toast.makeText(InitSetting.this, R.string.err_nw,
					Toast.LENGTH_SHORT).show();
			return;
		}
		EditText loginName = (EditText) findViewById(R.id.loginName);
		String name = loginName.getText().toString();
		Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
		Pattern pattern = Pattern.compile("\\w+");
		
		if (pattern.matcher(name).matches()) {
			Toast.makeText(InitSetting.this, R.string.notify_fill_usrname,
					Toast.LENGTH_SHORT).show();
			loginName.startAnimation(shake);
			return;
		}
		EditText loginPwd = (EditText) findViewById(R.id.loginPwd);
		String pwd = loginPwd.getText().toString();
		if (pattern.matcher(pwd).matches()) {
			Toast.makeText(InitSetting.this, R.string.notify_fill_passwod,
					Toast.LENGTH_SHORT).show();
			loginPwd.startAnimation(shake);
			return;
		}
		Wima.getInstance().getTracer().setLoginInfo(name, pwd);
		dialog = ProgressDialog.show(this, "",
				getResources().getString(R.string.notify_registing), true);
	}

	public Handler getHandler() {
		return handler;
	}

	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			if (dialog != null)
				dialog.dismiss();
			LocalData data = Wima.getInstance().getLocalData();
			Intent service = new Intent(Wima.getInstance()
					.getApplicationContext(), TraceService.class);
			switch (msg.what) {
			case Tracer.ON_REGISTED:
				Toast.makeText(InitSetting.this, R.string.notify_register_ok,
						Toast.LENGTH_LONG).show();
				data.save("registed", "1");
				startService(service);
				finish();
				break;
			case Tracer.ON_HAVE_REGISTED:
				Toast.makeText(InitSetting.this, R.string.reregister_notes,
						Toast.LENGTH_LONG).show();
				data = Wima.getInstance().getLocalData();
				data.save("registed", "1");
				startService(service);
				finish();
				break;
			case Tracer.ON_REGIST_FAILED:
				Toast.makeText(InitSetting.this,
						R.string.notify_register_failed, Toast.LENGTH_SHORT)
						.show();
				break;
			case Tracer.ON_INTERNAL_ERR:
				Toast.makeText(InitSetting.this, "Internal error",
						Toast.LENGTH_SHORT).show();
				break;
			}
		}
	};

	@Override
	protected void onStart() {
		super.onStart();
		Wima.getInstance().getTracer().register(this);
		showDialog(DIALOG_HELP);
	}

	@Override
	protected void onStop() {
		super.onStop();
		Wima.getInstance().getTracer().deregister(this);
	}

	public final static int DIALOG_HELP = 0;

	protected Dialog onCreateDialog(int id) {
		AlertDialog.Builder dialog = null;
		switch (id) {
		case DIALOG_HELP:
			dialog = new AlertDialog.Builder(InitSetting.this);
			dialog.setTitle(R.string.help_title);
			dialog.setMessage(R.string.help_text);
			dialog.setPositiveButton(R.string.ok_button,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
							dialog.dismiss();
						}
					});
			break;

		}
		if (dialog != null) {
			return dialog.create();
		} else {
			return null;
		}
	}

}
